/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.table.editor;

import com.rw.table.TableInfo;
import com.rw.table.formatter.ICellFormatter;
import java.awt.Component;
import java.text.ParseException;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author Rene Tapia
 */
public class SpinnerCellEditor extends ACellEditor {

    private JSpinner spinner;

    public SpinnerCellEditor(int value, int min, int max, int step) {
        SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, step);
        this.spinner = new JSpinner(model);
        this.spinner.setOpaque(true);
    }

    @Override
    public Component getComponent(TableInfo info) {
        Object value = info.getValue();
        if (value != null) {
            int intValue = ((Integer) value).intValue();
            spinner.setValue(intValue);
        }
        return spinner;
    }

    @Override
    public Object getCellEditorValue(ICellFormatter formatter) {
        try {
            this.spinner.commitEdit();
            return spinner.getValue();
        } catch (ParseException ex) {
//            ex.printStackTrace();
            return null;
        }
    }
}
